package com.example.config.loadbalanced;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@Component
public class ServiceCaller {

    @Autowired
    @Qualifier("loadBalancedRestTemplate")
    private RestTemplate restTemplate;

    /**
     * 通用 GET 请求
     */
    public <T> T get(String serviceName, String path, Class<T> responseType, Object... uriVariables) {
        String url = buildUrl(serviceName, path);

        ResponseEntity<T> response = restTemplate.exchange(
                url,
                HttpMethod.GET,
                null,
                responseType,
                uriVariables
        );

        return response.getBody();
    }

    /**
     * 通用 GET 请求（支持复杂返回类型）
     */
    public <T> T get(String serviceName, String path, ParameterizedTypeReference<T> responseType,
                     Map<String, ?> uriVariables) {
        String url = buildUrl(serviceName, path);

        ResponseEntity<T> response = restTemplate.exchange(
                url,
                HttpMethod.GET,
                null,
                responseType,
                uriVariables
        );

        return response.getBody();
    }

    /**
     * 通用 POST 请求
     */
    public <T, R> T post(String serviceName, String path, R requestBody, Class<T> responseType) {
        String url = buildUrl(serviceName, path);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        HttpEntity<R> entity = new HttpEntity<>(requestBody, headers);

        ResponseEntity<T> response = restTemplate.exchange(
                url,
                HttpMethod.POST,
                entity,
                responseType
        );

        return response.getBody();
    }

    /**
     * 通用 PUT 请求
     */
    public <T, R> T put(String serviceName, String path, R requestBody, Class<T> responseType, Object... uriVariables) {
        String url = buildUrl(serviceName, path);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        HttpEntity<R> entity = new HttpEntity<>(requestBody, headers);

        ResponseEntity<T> response = restTemplate.exchange(
                url,
                HttpMethod.PUT,
                entity,
                responseType,
                uriVariables
        );

        return response.getBody();
    }

    /**
     * 通用 DELETE 请求
     */
    public void delete(String serviceName, String path, Object... uriVariables) {
        String url = buildUrl(serviceName, path);

        restTemplate.exchange(
                url,
                HttpMethod.DELETE,
                null,
                Void.class,
                uriVariables
        );
    }

    private String buildUrl(String serviceName, String path) {
        if (!path.startsWith("/")) {
            path = "/" + path;
        }
        return "http://" + serviceName + path;
    }
}
